package com.example.mybatisplus.web.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.DutyTimeMapper;
import com.example.mybatisplus.model.domain.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.DutyTimeService;


/**
 *
 *  前端控制器
 *
 *
 * @author cft
 * @since 2022-06-29
 * @version v1.0
 */
@Controller
@RequestMapping("/api/dutyTime")
public class DutyTimeController {

    private final Logger logger = LoggerFactory.getLogger(DutyTimeController.class);

    @Autowired
    private DutyTimeService dutyTimeService;

    /**
     * 描述：根据Id 查询
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
        DutyTime dutyTime = dutyTimeService.getById(id);
        return JsonResponse.success(dutyTime);
    }


    /**
     * 描述：根据Id删除
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        dutyTimeService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateDutyTime(DutyTime dutyTime) throws Exception {
        dutyTimeService.updateById(dutyTime);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建DutyTime
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
	public JsonResponse create(DutyTime dutyTime) throws Exception {
		dutyTimeService.save(dutyTime);
		return JsonResponse.success(null);
	}


	/*
	 * author:cft
	 * function:中心管理员配置值班时间段的老师
	 */
	@RequestMapping("/alterOnDutyTeacher")
	@ResponseBody
	public JsonResponse alterOnDutyTeacher(@RequestBody DutyTime dutyTime) {
		dutyTimeService.alterDutyTime(dutyTime);
		return JsonResponse.success("success");
	}

	/*
	 * author:cft
	 * function:获得值班时间表的所有信息
	 */
	@RequestMapping("/getAllDutyInfo")
	@ResponseBody
	public JsonResponse getAllDutyInfo() {
		return JsonResponse.success(dutyTimeService.list());
	}

	/*
	 * author:cft
	 * function:根据id查询
	 */
	@RequestMapping("/getTeaAndPlace")
	@ResponseBody
	public JsonResponse getTeaAndPlace(@RequestBody DutyTime dutyTime) {

		return JsonResponse.success(dutyTimeService.getById(dutyTime.getPeriodId()));
	}

	/*
	 * author:cft
	 * function:中心管理员修改初访员值班状态
	 */
	@RequestMapping("/getFviewerBusy")
	@ResponseBody
	public JsonResponse getFviewerBusy(@RequestBody DutyTime dutyTime) {
		dutyTime.setDutyStatus("忙碌");
		dutyTimeService.updateById(dutyTime);
		return JsonResponse.success("success");
	}


	/*
	* author:tqq
	* 根据初访结果数组 中的值班时间段的id获取值班时间信息
	* */
    @PostMapping("/getPeriodById")
    @ResponseBody
    public JsonResponse getPeriodById(@RequestBody  FirstApoStatus firstApoStatuses[]){
        DutyTime []dutyTime =new DutyTime[firstApoStatuses.length];
        for (int i = 0; i < firstApoStatuses.length; i++) {
            dutyTime[i] = dutyTimeService.getById(firstApoStatuses[i].getFSelecPeriod());
        }
        return JsonResponse.success(dutyTime);
    }

    /*
    * author:tqq
    * 根据周几查询空闲的时间段
    * */
    @PostMapping("/disFreeDutyTime")
    @ResponseBody
	public JsonResponse disFreeDutyTime(@RequestBody JSONObject json) {
		int str = Integer.parseInt(json.getString("day"));
		System.out.println("day is :" + str);
		List<DutyTime> list = dutyTimeService.getFreeDutyTimeByDay(str);
		return JsonResponse.success(list);
	}


    /*
     * author:jrh
     * function:查询初访员空闲值班时间表
     */
    @RequestMapping("/getFViewerDuty")
    @ResponseBody
    public JsonResponse getFViewerDuty(){
        List<DutyTime> list=dutyTimeService.getFViewerDuty();
        return JsonResponse.success(list);
    }

    /*
     * author:tqq
     * 根据咨询记录数组 中的值班时间段的id获取值班时间信息
     * */
    @PostMapping("/getPeriodByconPeriodId")
    @ResponseBody
	public JsonResponse getPeriodByconPeriodId(@RequestBody ConRecord conRecords[]) {
		DutyTime[] dutyTime = new DutyTime[conRecords.length];
		for (int i = 0; i < conRecords.length; i++) {
			dutyTime[i] = dutyTimeService.getById(conRecords[i].getConPeriodId());
		}
		return JsonResponse.success(dutyTime);
	}
	/*
     * author:jrh
     * function:查询初访员值班时间表
     */
    @RequestMapping("/getFDuty")
    @ResponseBody
    public JsonResponse getFDuty(){
        List<DutyTime> list=dutyTimeService.getFDuty();
        return JsonResponse.success(list);
    }

	/*
	 * author:jrh
	 * function:初访根据periodId查询对应时间
	 */
	@Autowired
	DutyTimeMapper dutyTimeMapper;
	@PostMapping("/getTime")
	@ResponseBody
	public JsonResponse getTime(@RequestBody JSONObject jsonObject) {
		Long id=Long.parseLong(jsonObject.getString("fapoId"));
		List<DutyTime> list=dutyTimeMapper.getTime(id);
		return JsonResponse.success(list);
	}

	/*
	 * author:jrh
	 * function:追加咨询根据periodId查询对应时间
	 */
	@PostMapping("/getTime2")
	@ResponseBody
	public JsonResponse getTime2(@RequestBody JSONObject jsonObject) {
		Long id=Long.parseLong(jsonObject.getString("conPeriodId"));
		List<DutyTime> list=dutyTimeMapper.getTime2(id);
		return JsonResponse.success(list);
	}

	/*
	 * author:jrh
	 * function:查询咨询师值班时间表
	 */
	@RequestMapping("/getConTime")
	@ResponseBody
	public JsonResponse getConTime(){
		List<DutyTime> list=dutyTimeService.getConTime();
		return JsonResponse.success(list);
	}


}




//

